<template>
  <div class="app-layout">
    <div class="app-layout-main">
      <!-- 侧边栏 -->
      <Sidebar />
      
      <!-- 主内容区 -->
      <div class="app-layout-content">
        <!-- 顶部栏 -->
        <div class="top-bar">
          <!-- 左侧区域：菜单折叠按钮和面包屑 -->
          <div class="top-bar-left">
            <Button 
              icon="pi pi-bars" 
              text 
              rounded 
              size="small"
              @click="layoutStore.toggleSidebar"
              :aria-label="layoutStore.sidebarCollapsed ? '展开菜单' : '折叠菜单'"
              class="menu-toggle-button"
            />
            
            <!-- 面包屑导航 -->
            <div class="top-bar-breadcrumb">
              <Breadcrumb>
                <template v-for="(item, index) in breadcrumbs" :key="index">
                  <BreadcrumbItem>
                    <router-link v-if="index < breadcrumbs.length - 1" :to="item.path">
                      {{ item.meta?.title }}
                    </router-link>
                    <span v-else>{{ item.meta?.title }}</span>
                  </BreadcrumbItem>
                </template>
              </Breadcrumb>
            </div>
          </div>
          
          <!-- 右侧区域：功能按钮 -->
          <div class="top-bar-right">
            <!-- 通知组件 -->
            <Notification />
            
            <!-- 主题切换组件 -->
            <ThemeSelect />
            
            <!-- 语言切换组件 -->
            <LanguageSelect />
            
            <!-- 用户信息 -->
            <div class="user-info">
              <i class="pi pi-user user-icon"></i>
              <span class="user-name">{{ userStore.userInfo?.nickname || userStore.userInfo?.username || 'admin' }}</span>
              <Button 
                icon="pi pi-chevron-down" 
                text 
                rounded 
                size="small"
                @click="toggleUserMenu"
                class="user-menu-button"
              />
              <div v-if="showUserMenu" class="user-dropdown-menu">
                <div class="menu-item" @click="goToProfile">个人中心</div>
                <div class="menu-item" @click="goToSettings">设置</div>
                <div class="menu-separator"></div>
                <div class="menu-item text-red-500" @click="handleLogout">退出登录</div>
              </div>
            </div>
          </div>
        </div>
        
        <!-- 多页签 -->
        <Tabs />
        
        <!-- 页面内容 -->
        <div class="page-content">
          <router-view />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
// 使用相对路径替代别名路径
import { useLayoutStore } from '../store/modules/layout'
import { useMenuStore } from '../store/modules/menu'
import { useUserStore } from '../store/modules/user'
import { useI18nStore } from '../store/modules/i18n'
import { useTabsStore } from '../store/modules/tabs'
import Button from 'primevue/button'
import Breadcrumb from 'primevue/breadcrumb'
import Sidebar from './components/Sidebar.vue'
import Tabs from '../components/Tabs/index.vue'
import Notification from '../components/Notification/index.vue'
import ThemeSelect from '../components/ThemeSelect/index.vue'
import LanguageSelect from '../components/LanguageSelect/index.vue'

// 初始化 store
const router = useRouter()
const layoutStore = useLayoutStore()
const menuStore = useMenuStore()
const userStore = useUserStore()
const i18nStore = useI18nStore()
const tabsStore = useTabsStore()


// 默认头像
const defaultAvatar = 'https://via.placeholder.com/32'

// 用户菜单显示状态
const showUserMenu = ref(false)

// 切换用户菜单显示
const toggleUserMenu = () => {
  showUserMenu.value = !showUserMenu.value
}

// 菜单项处理函数
const goToProfile = () => {
  showUserMenu.value = false
  router.push('/profile')
}

const goToSettings = () => {
  showUserMenu.value = false
  router.push('/settings')
}

const handleLogout = () => {
  showUserMenu.value = false
  userStore.logout()
}

// 初始化
onMounted(async () => {
  // 初始化国际化
  i18nStore.initLocale()
  
  // 初始化用户信息
  await userStore.initUserInfo()
  
  // 初始化布局
  layoutStore.initLayout()
  
  // 初始化菜单
  await menuStore.initMenus()
  
  // 初始化页签
  tabsStore.initHomeTab()
  
  // 设置当前激活的菜单
  const currentRoute = router.currentRoute.value
  menuStore.updateActiveMenu(currentRoute.path)
  
  // 为当前路由创建或激活页签
  const currentTab = tabsStore.createTabFromRoute(currentRoute)
  tabsStore.addTab(currentTab)
  
  // 监听路由变化，更新激活的菜单和页签
  router.afterEach((to) => {
    menuStore.updateActiveMenu(to.path)
    
    // 为当前路由创建或激活页签
    const tab = tabsStore.createTabFromRoute(to)
    tabsStore.addTab(tab)
  })
  
  // 点击外部关闭用户菜单
  document.addEventListener('click', (e) => {
    const userInfoEl = document.querySelector('.user-info')
    if (userInfoEl && !userInfoEl.contains(e.target as Node)) {
      showUserMenu.value = false
    }
  })
})
</script>

<style scoped>
.app-layout {
  display: flex;
  width: 100%;
  height: 100vh;
  overflow: hidden;
  background-color: #f0f2f5;
}

.app-layout-main {
  display: flex;
  flex: 1;
  overflow: hidden;
}

.app-layout-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background-color: #f0f2f5;
}

/* 顶部栏样式 */
.top-bar {
  height: 64px;
  background-color: #fff;
  border-bottom: 1px solid #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  position: relative;
  z-index: 10;
}

.top-bar-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

/* 顶部栏面包屑样式 */
.top-bar-breadcrumb {
  display: flex;
  align-items: center;
}

.top-bar-breadcrumb :deep(.p-breadcrumb) {
  background: transparent;
  border: none;
  padding: 0;
}

.top-bar-breadcrumb :deep(.p-breadcrumb .p-breadcrumb-list) {
  margin: 0;
  padding: 0;
}

.top-bar-breadcrumb :deep(.p-breadcrumb .p-breadcrumb-item) {
  color: #666;
  font-size: 14px;
}

.top-bar-breadcrumb :deep(.p-breadcrumb .p-breadcrumb-item a) {
  color: #666;
  text-decoration: none;
  transition: color 0.3s;
}

.top-bar-breadcrumb :deep(.p-breadcrumb .p-breadcrumb-item a:hover) {
  color: #1890ff;
}

.top-bar-breadcrumb :deep(.p-breadcrumb .p-breadcrumb-item:last-child) {
  color: #333;
  font-weight: 500;
}

.top-bar-breadcrumb :deep(.p-breadcrumb .p-breadcrumb-separator) {
  color: #ccc;
  margin: 0 8px;
}

.top-bar-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

/* 菜单折叠按钮样式 */
.menu-toggle-button {
  color: #666 !important;
  border: none !important;
  background: transparent !important;
  padding: 8px !important;
  width: 40px !important;
  height: 40px !important;
}

.menu-toggle-button:hover {
  color: #1890ff !important;
  background-color: #f0f8ff !important;
}

/* 用户信息样式 */
.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  position: relative;
  padding: 8px 12px;
  border-radius: 6px;
  transition: background-color 0.3s;
  gap: 8px;
}

.user-info:hover {
  background-color: #f5f5f5;
}

.user-icon {
  font-size: 16px;
  color: #666;
  width: 16px;
  height: 16px;
  text-align: center;
  display: inline-block;
  line-height: 1;
}

.user-name {
  font-size: 14px;
  color: #333;
  font-weight: 500;
}

.user-menu-button {
  color: #666 !important;
  border: none !important;
  background: transparent !important;
  padding: 4px !important;
  margin-left: 4px !important;
}

.user-menu-button:hover {
  color: #1890ff !important;
  background-color: #f0f8ff !important;
}

.user-dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  background: white;
  border: 1px solid #f0f0f0;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  min-width: 160px;
  margin-top: 8px;
  z-index: 1000;
  overflow: hidden;
}

.menu-item {
  padding: 12px 16px;
  cursor: pointer;
  transition: background-color 0.2s;
  font-size: 14px;
  color: #333;
  display: flex;
  align-items: center;
}

.menu-item:hover {
  background-color: #f5f5f5;
}

.menu-item.text-red-500 {
  color: #ff4d4f;
}

.menu-item.text-red-500:hover {
  background-color: #fff2f0;
}

.menu-separator {
  height: 1px;
  background-color: #f0f0f0;
  margin: 4px 0;
}

.page-content {
  flex: 1;
  padding: 24px;
  overflow: auto;
  background-color: #fff;
  margin: 24px;
  border-radius: 8px;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}
</style>